import React, { Component } from 'react'
import Header from '../../components/Header/Header'
import { Image, List,Button, Toast  } from 'antd-mobile'
import {reqgood} from '../../request/api'
import {imgPre} from "../../request/http"
import './Lists.less'
import {reqcartadd} from '../../request/api'

export default class Lists extends Component {
    constructor(){
        super();
        this.state={
            goods:[]
        }
    }
    componentDidMount(){
        let params=this.props.match.params;
        reqgood({fid:params.id}).then(d=>{
            if(d.data.code === 200){
                this.setState({
                    goods:d.data.list
                })
            }

        })
    }
    cartadd(e,item){
        e.stopPropagation();
        let obj =sessionStorage.getItem("isLogin");
        let objp = JSON.parse(obj);
        reqcartadd({uid:objp.uid,type:1,goodsid:item.id,num:1}).then(d=>{
            Toast.show({content:d.data.msg})
        })
    }
    render() {
        let {goods} = this.state
        return (
            <div>
                <Header title="小U优选" back></Header>
                <div className="lists">
                    <List>
                        {goods.map(item => (
                            <List.Item
                            onClick={key=>{this.props.history.push("/Detail/"+item.id)}}
                            key={item.id}
                            prefix={
                                <Image
                                src={imgPre+item.img}
                                fit='cover'
                                width={80}
                                height={80}
                                />
                            }
                            description={item.description}
                            >
                            {item.goodsname}
                            <div>
                                原价<del>{item.market_price}</del>
                                &nbsp;&nbsp;&nbsp;
                                <span>现价：{item.price}</span>
                            </div>
                            <Button color='warning' onClick={(e)=>{this.cartadd(e,item)}}>加入购物车</Button>
                            </List.Item>
                        ))}
                    </List>
                </div>
            </div>
        )
    }
}
